package com.qf.joint.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Share;
import com.qf.joint.service.ShareService;
import com.qf.joint.utils.Info;
import com.qf.joint.utils.Uploadword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author H
 * Date on 2020/9/13  16:37
 * @description
 */
@Controller
@RequestMapping("/share")
@CrossOrigin
public class ShareController {
    @Autowired
    private ShareService shareService;

    //前台查询所有
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    @ResponseBody
    public Info<Share> selectAll1() {
        List<Share> list = shareService.selectAll();
        Info<Share> info = new Info<>();
        info.setCode(0);
        info.setData(list);
        info.setMsg("success");
        info.setCount(list.size());//总数
        System.out.println(info);
        return info;//-/WEB-INF/view/test.jsp
    }


    @RequestMapping(value = "/findlike", method = RequestMethod.GET)
    @ResponseBody
    public List<Share> selectByCon(String condition) throws UnsupportedEncodingException {

        condition = new String(condition.getBytes("ISO-8859-1"), "UTF-8");

        List<Share> list = shareService.selectByCon(condition);


        System.out.println(list);

        return list;
    }


    //后台
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public Info<Share> selectAll(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        PageHelper.startPage(page, limit);
        List<Share> list = shareService.selectAll();
        PageInfo<Share> pageInfo = new PageInfo<>(list);
        //PageInfo pageInfo=new PageInfo(list);
        Info<Share> info = new Info<>();
        if (null != list && list.size() > 0) {
            info.setCode(0);
            info.setData(pageInfo.getList());
            //总数
            info.setCount(shareService.selectCount());
            System.out.println("controller1-info=" + info);
            return info;
        }
        info.setCode(1);
        info.setMsg("没有查询到数据");
        System.out.println("controller2-info=" + info);
        //-/WEB-INF/view/test.jsp
        return info;
    }

    //删除
    @RequestMapping(value = "/deleteOne", method = RequestMethod.GET)
    public String deleteOne(@RequestParam(name = "idArry", required = true) String idArry) {
        String[] idlist = idArry.split(",");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < idlist.length; i++) {
            list.add(idlist[i]);
        }
        int deleteOne = shareService.deleteOne(list);
        System.out.println(deleteOne);
        return "redirect:/admin/admin";
    }

    //增加
    @RequestMapping(value = "/insertOne", method = RequestMethod.POST)
    public String insertOne(Share share) {
        int insertOne = shareService.insertOne(share);
        System.out.println(insertOne);
        return "redirect:/admin/admin";
    }

    //根据ID查询
    @RequestMapping("selectById/{id}")
    @ResponseBody
    public Share selectById(@PathVariable("id") int id) {
        Share share = shareService.selectById(id);
        System.out.println(share);
        return share;
    }


    @RequestMapping(value = "/updateOne", method = RequestMethod.POST)
    @ResponseBody
    public Info<Share> updateOne(Share share) {
        int update = shareService.updateOne(share);
        Info<Share> info = new Info<Share>();
        info.setCode(update);
        /*if (update == 1) {
            return "修改成功";
        }
        return "修改失败";*/
        return info;

    }

    @RequestMapping(value = "/sc", method = RequestMethod.POST)
    public String insertOne(Share share, MultipartFile pic, HttpSession session) throws IOException {
        Uploadword uploadword = new Uploadword();
        String update = uploadword.update(pic, session);
        System.out.println("地址" + update);
        share.setLogo(update);
        shareService.insertOne(share);

        return "redirect:/admin/admin";

    }


}
